// 题库管理-章节管理
app.controller('chapterManage', ['$scope','$modal','$http','$rootScope','$state', function($scope,$modal,$http,$rootScope,$state){
	// 获取分类列表
	$scope.getList=function(){
		$http({
		    url:$rootScope.baseUrl+"ExercisesChapter/getList",
		    method:"post",
		}).then(function(res){
		    if(res.data.code==200){
		    	$scope.exercisesChapterList=res.data.data;
		    	
		    	if($scope.exercisesChapterList.length==0){
		    		$scope.noData=true;
		    	}else {
		    		$scope.noData=false;
		    	}
		    }else if(res.data.code==409){
		    	$state.go("access.signin");
		    }
		},function(error){
		    console.log(error);
		});
	}
	$scope.getList();

	//新建章节
	$scope.addChapter=function(){
		var modalInstance = $modal.open({
		    templateUrl : 'addChapter.html',  //指向创建的视图
		    controller : 'addChapter',// 初始化模态范围
		    size : "sm",
		    backdrop:"static"
		});

		modalInstance.result.then(function() {
			$scope.getList();
		}, function() {
			$scope.getList();
		});
	}

	//编辑章节
	$scope.editChapter=function(item){
		var modalInstance = $modal.open({
		    templateUrl : 'editChapter.html',  //指向创建的视图
		    controller : 'editChapter',// 初始化模态范围
		    size : "sm",
		    backdrop:"static"
		});

		modalInstance.result.then(function() {
			$scope.getList();
		}, function() {
			$scope.getList();
		});

		$rootScope.chapter=item;
	}

	// 删除章节
	$scope.deleteChapter=function(item){
		swal({
		    title: "确定删除？",
		    type: "warning",
		    showCancelButton: true,
		    confirmButtonColor: "#619ce1",
		    confirmButtonText: "确定",
		    closeOnConfirm: false
		},
		function() {
		    $http({
		        url:$rootScope.baseUrl+"ExercisesChapter/delAction",
		        method:"post",
		        data:{
		        	id:item.id
		        }
		    }).then(function(res){
		        if(res.data.code==200){
		        	swal("删除成功", "", "success");
		        	$scope.getList();
		        }else if(res.data.code==409){
			    	$state.go("access.signin");
			    }
		    },function(error){
		        console.log(error);
		    });
		});
	}
}]);

// 新建章节
app.controller('addChapter', ['$scope','$http','$rootScope','$modalInstance', '$state',function($scope,$http,$rootScope,$modalInstance,$state){
	// 取消
	$scope.cancelAdd=function(){
		$modalInstance.close();
	}

	$scope.imgList=[
		{
			name:"时间题"
		},
		{
			name:"距离题"
		},
		{
			name:"罚款题"
		},
		{
			name:"速度题"
		},
		{
			name:"标线题"
		},
		{
			name:"标志题"
		},
		{
			name:"计分题"
		},
		{
			name:"手势题"
		},
		{
			name:"信号题"
		},
		{
			name:"酒驾题"
		},
		{
			name:"灯光题"
		},
		{
			name:"仪表题"
		},
		{
			name:"装置题"
		},
		{
			name:"路况题"
		}
	];

	$scope.selectIcon=function(index){
		$scope.imgNum=index+1;
	}

	// 确定
	$scope.sureAdd=function(){
		if($scope.name==undefined || $scope.imgNum==undefined){
			swal("请填写完整的信息");
		}else {
			$http({
			    url:$rootScope.baseUrl+"ExercisesChapter/addAction",
			    method:"post",
			    data:{
			    	name:$scope.name,
			    	img_num:$scope.imgNum
			    }
			}).then(function(res){
			    if(res.data.code==200){
			    	swal({
			    	    title: "添加成功",
			    	    type: "success",
			    	    showCancelButton: false,
			    	    confirmButtonColor: "#619ce1",
			    	    confirmButtonText: "确定",
			    	    closeOnConfirm: true
			    	},
			    	function() {
			    	    $modalInstance.close();
			    	});
			    }else if(res.data.code==409){
			    	$state.go("access.signin");
			    }
			},function(error){
			    console.log(error);
			});
		}
	}
}]);

// 编辑章节
app.controller('editChapter', ['$scope','$http','$rootScope','$modalInstance','$state', function($scope,$http,$rootScope,$modalInstance,$state){
	$scope.chapter=$rootScope.chapter;
	console.log($scope.chapter);
	// 取消
	$scope.cancelAdd=function(){
		$modalInstance.close();
	}

	$scope.imgList=[
		{
			name:"时间题"
		},
		{
			name:"距离题"
		},
		{
			name:"罚款题"
		},
		{
			name:"速度题"
		},
		{
			name:"标线题"
		},
		{
			name:"标志题"
		},
		{
			name:"计分题"
		},
		{
			name:"手势题"
		},
		{
			name:"信号题"
		},
		{
			name:"酒驾题"
		},
		{
			name:"灯光题"
		},
		{
			name:"仪表题"
		},
		{
			name:"装置题"
		},
		{
			name:"路况题"
		}
	];

	$scope.selectIcon=function(index){
		$scope.chapter.img_num=index+1;
	}

	// 确定
	$scope.sureAdd=function(){
		if($scope.chapter.name==undefined || $scope.chapter.img_num==undefined){
			swal("请填写完整的信息");
		}else {
			$http({
			    url:$rootScope.baseUrl+"ExercisesChapter/editAction",
			    method:"post",
			    data:{
			    	name:$scope.chapter.name,
			    	img_num:$scope.chapter.img_num,
			    	id:$scope.chapter.id
			    }
			}).then(function(res){
			    if(res.data.code==200){
			    	swal({
			    	    title: "编辑成功",
			    	    type: "success",
			    	    showCancelButton: false,
			    	    confirmButtonColor: "#619ce1",
			    	    confirmButtonText: "确定",
			    	    closeOnConfirm: true
			    	},
			    	function() {
			    	    $modalInstance.close();
			    	});
			    }else if(res.data.code==409){
			    	$state.go("access.signin");
			    }
			},function(error){
			    console.log(error);
			});
		}
	}
}]);